package com.browser;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



import java.util.ArrayList;
import java.util.List;

/**
 * Created by fudong2 on 2015/8/5.
 */
public class DetailPicsActivity extends Activity implements ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;
    private PicPagerAdapter adapter;
    private TextView numText;
    private static List<String> pics;
    private static int currPosition = 0;
    private static String curOriginalImageType = "";
    private DetailPicsActivity mContext;

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (null != pics && pics.size() > 1) {
            numText.setText((position + 1) + "/" + pics.size());
            numText.invalidate();
        }
        if (adapter != null) {
            int p1 = position - 1;
            if (p1 >= 0 && null != adapter.getItemView(p1)) {
                adapter.getItemView(p1).resetMatrix();
            }
            int p2 = position + 1;
            if (p2 < pics.size() && null != adapter.getItemView(p2)) {
                adapter.getItemView(p2).resetMatrix();
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    class PicPagerAdapter extends PagerAdapter {
        private List<?> mList;
        private SparseArray<DetailPicItemView> mViews;

        public PicPagerAdapter(List<?> list) {
            mList = list;
            mViews = new SparseArray<>(mList.size());
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            final Object item = mList.get(position);
            if (item instanceof String) {
                final DetailPicItemView photoView = new DetailPicItemView(container.getContext(),true,curOriginalImageType);
                photoView.update((String) item);
                mViews.put(position, photoView);
                // Now just add PhotoView to ViewPager and return it
                container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams
                        .MATCH_PARENT);
                return photoView;
            }

            return null;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        public DetailPicItemView getItemView(int position) {
            return mViews.get(position);
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_pics);
        mContext = this;
        mViewPager = (ViewPager) findViewById(R.id.pics);
        numText = (TextView) findViewById(R.id.pic_num);
        if (null != pics && pics.size() > 0) {
            adapter = new PicPagerAdapter(pics);
            mViewPager.setAdapter(adapter);
            mViewPager.setOnPageChangeListener(this);
            if (null != pics && pics.size() > 1) {
                numText.setVisibility(View.VISIBLE);
                numText.setText(1 + "/" + pics.size());
            } else {
                numText.setVisibility(View.GONE);
            }
            mViewPager.setCurrentItem(currPosition);

//            final List<String> pictureUrls = new ArrayList<>();
//            for (int i =0; i < pics.size(); i++){
//                BrowserPicItem item = pics.get(i);
//                pictureUrls.add(item.getPicUrl());
//            }
        } else
            finish();
    }

    @Override
    protected void onResume() {
        super.onResume();

//        if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        pics = null;
    }

    public static void jumpToThis(Activity activity, List<String> list,int position) {
        DetailPicsActivity.pics = list;
        currPosition = position;
//        curOriginalImageType = originalImageType;

        Intent intent = new Intent(activity, DetailPicsActivity.class);
        activity.startActivity(intent);
    }

}
